package com.course.system.controller.admin;

import com.course.model.dto.RoleUserDto;
import com.course.model.dto.PageDto;
import com.course.common.response.BaseResponse;
import com.course.model.util.ValidatorUtil;
import com.course.model.service.RoleUserService;
import com.course.system.controller.BaseController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/roleUser")
public class RoleUserController extends BaseController {

    public static final String BUSINESS_NAME = "角色用户关联";

    @Resource
    private RoleUserService roleUserService;

    /**
     * 列表查询
     */
    @PostMapping("/getRoleUserList")
    public BaseResponse list(@RequestBody PageDto pageDto) {
        BaseResponse response = new BaseResponse().sucess();
        roleUserService.list(pageDto);
        response.setData(pageDto);
        return response;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody RoleUserDto roleUserDto) {
        // 保存校验
        ValidatorUtil.require(roleUserDto.getRoleId(), "角色");
        ValidatorUtil.require(roleUserDto.getUserId(), "用户");

        BaseResponse response = new BaseResponse().sucess();
        roleUserService.save(roleUserDto);
        response.setData(roleUserDto);
        return response;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        roleUserService.delete(id);
        return response;
    }
}
